/*
 * @Author: tyz
 * @Date: 2021-06-24 17:03:02
 * @LastEditTime: 2021-06-24 17:27:04
 * @Description: jwt自定义策略
 * @FilePath: \nest_demo\src\module\auth\jwt.strategy.ts
 */
import { Strategy, ExtractJwt } from 'passport-jwt';
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';

/*
    * 自定义jwt策略步骤：
    *   1. 上面的import都需要引入下来
    *   2. 自定义'JwtStrategy'通过继承'PassportStrategy'来实现自定义策略，另外还需要传递'Strategy'参数
    *   3. 定义validate方法
*/

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
    constructor() {
        super({
            jwtFromRequest: ExtractJwt.fromHeader('token'),
            ignoreExpiration: false,
            secretOrKey: 'secretKey',
        })
    };

    async validate(userInfo:any) {
        console.log("jwt解析结果：",userInfo)
        return { userInfo };
    }
}   